using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace Sys.DataAccess
{
    //A query can return multiple tables as results. Each table might potentially contain data
    //which creates a new instance. Hence multiple ObjectConstructionInfo are required. The first element
    //in the list will be DataSet.Tables[0], the next will be Tables[1] and so on.
    public class QueryResultMap : Dictionary<int, ObjectConstructionInfo>
    {
        private string connectionString;
        public string ConnectionString
        {
            get { return connectionString; }
        }

        private string commandText;
        public string CommandText
        {
            get { return commandText; }
        }
        
        //You cannot create this class from outside.
        private QueryResultMap(string connectionString, string commandText)
        {
            this.connectionString = connectionString;
            this.commandText = commandText;
        }

        //entries QueryResultMap for each connectionString + commandText combination        
        static List<QueryResultMap> entries = new List<QueryResultMap>();

        public static QueryResultMap Get(string connectionString, string commandText)
        {
            foreach (QueryResultMap map in entries)
            {
                if (map.ConnectionString == connectionString && map.CommandText == commandText) return map;
            }
            //Map was not found, we need to create.
            QueryResultMap newMap = new QueryResultMap(connectionString, commandText);
            entries.Add(newMap);
            return newMap;
        }

        public static void Remove(string connectionString, string commandText)
        {
            foreach (QueryResultMap map in entries)
            {
                if (map.ConnectionString == connectionString && map.CommandText == commandText)
                {
                    entries.Remove(map);
                    return;
                }
            }
        }
    }
}